<?php 
require_once('init.php');
$name = htmlescape(trim($_POST['name']));

if ( $name ) {

    $url = Systype::getCleanUrl( $name );
    
    if ( $url ) {
        $nodes = new Nodes();
        $node = $nodes->getNodeFromUrl( $name );
    } else {
        $node = new Node();
        $node->setName( $name );
        $node->setContent( $_POST['content'] );
        $node->save();
    }
    
    if ($node->getId()) {
        redirect(geturl($node->getName(),$node->getId()));
    } else {
        die('YOU BROKE IT AGAIN. I\'M CALLING THE POLICE!!');
    }
} else {
    $name = htmlescape(urldeescape($_GET['q']));
}

?>
    <form action="/post.php" method="POST" >
        <div>
            <label for="namefield">Nombre o URL</label><br />
            <input type="text" name="name" id="namefield" value="<?php echo $name; ?>" />
            <?php if (isset($url)) { ?>
                <input type="hidden" name="url" id="urlfield" value="<?php echo $url; ?>" />
            <?php } ?>
        </div>
        <span>Ingresa una breve descripción del nodo.</span>
        <div><textarea name="content" id="contentfield"><?php echo $content; ?></textarea></div>
        <div><input type="submit" class="submit" value="Crear" /></div>
    </form>
<?php

$options = array(
    'title' => $name
);
display($options);
